﻿Imports System.Net
Imports System.IO
Imports System.Text.RegularExpressions

Public Class Form1

    Dim updateurl As String = ""
    Dim updatetitle As String = ""
    Dim x As Integer = 0

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        If CheckedListBox1.CheckedItems.Count = 0 Then

            MsgBox("Select genres!")

        Else

            x = 0

            TextBox1.Clear()

            If RadioButton3.Checked = True Then

                If System.IO.Directory.Exists(TextBox2.Text) Then System.IO.Directory.Delete(TextBox2.Text, True)
                TextBox1.AppendText("Directory: " & TextBox2.Text & " deleted")
                TextBox1.AppendText(Environment.NewLine)
                If (Not System.IO.Directory.Exists(TextBox2.Text)) Then System.IO.Directory.CreateDirectory(TextBox2.Text)
                TextBox1.AppendText("Directory: " & TextBox2.Text & " created")
                TextBox1.AppendText(Environment.NewLine)
                TextBox1.AppendText(Environment.NewLine)

            End If

            Dim rgPattern = "[\\\/:\*\?""'<>|]"
            Dim objRegEx As New Regex(rgPattern)

            Dim i As Integer

            For i = 0 To CheckedListBox1.CheckedItems.Count - 1

                Dim getstation = getstring(CheckedListBox1.CheckedItems(i).ToString, NumericUpDown1.Value)

                If (Not System.IO.Directory.Exists(TextBox2.Text + "\" + CheckedListBox1.CheckedItems(i).ToString)) Then System.IO.Directory.CreateDirectory(TextBox2.Text + "\" + CheckedListBox1.CheckedItems(i).ToString)

                TextBox1.AppendText("Directory: " + CheckedListBox1.CheckedItems(i).ToString & " created")
                TextBox1.AppendText(Environment.NewLine)
                TextBox1.AppendText(Environment.NewLine)

                Dim gettitles = gettitle()

                If gettitles <> "Not Found" Then

                    gettitles = objRegEx.Replace(gettitles, "")


                    If gettitles.Length > 45 Then gettitles = gettitles.Substring(0, 45)
                    TextBox1.AppendText("Station found: " + gettitles)
                    TextBox1.AppendText(Environment.NewLine)

                    Dim wc As New System.Net.WebClient
                    wc.DownloadFile("http://yp.shoutcast.com/sbin/tunein-station.pls?id=" + getstation, TextBox2.Text + "\" + CheckedListBox1.CheckedItems(i).ToString + "\" + gettitles + ".pls")
                    wc.Dispose()

                    TextBox1.AppendText("Id: " + getstation + " - Saved successfully")
                    TextBox1.AppendText(Environment.NewLine)
                    x = x + 1

                Else

                    TextBox1.AppendText(Environment.NewLine)

                End If

                Dim populatelist As String

                Do
                    populatelist = getnext()

                    If populatelist <> "Not Found" Then

                        gettitles = gettitle()

                        gettitles = objRegEx.Replace(gettitles, "")

                        If gettitles.Length > 45 Then gettitles = gettitles.Substring(0, 45)
                        TextBox1.AppendText("Station found: " + gettitles)
                        TextBox1.AppendText(Environment.NewLine)

                        Dim wd As New System.Net.WebClient
                        wd.DownloadFile("http://yp.shoutcast.com/sbin/tunein-station.pls?id=" + populatelist, TextBox2.Text + "\" + CheckedListBox1.CheckedItems(i).ToString + "\" + gettitles + ".pls")
                        wd.Dispose()

                        TextBox1.AppendText("Id: " + populatelist + " - Saved successfully")
                        TextBox1.AppendText(Environment.NewLine)
                        x = x + 1

                    Else

                        TextBox1.AppendText(Environment.NewLine)

                    End If

                Loop Until populatelist = "Not Found"

            Next

            TextBox1.AppendText("Complete: " & x & " playlist files downloaded")
            TextBox1.AppendText(Environment.NewLine)

        End If

    End Sub

    Function GetPage(ByVal pageUrl As String) As String

        Dim s As String = ""

        Try

            Dim request As HttpWebRequest = WebRequest.Create(pageUrl)

            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C)"

            Dim response As HttpWebResponse = request.GetResponse()

            Using reader As StreamReader = New StreamReader(response.GetResponseStream())

                s = reader.ReadToEnd()
                reader.Dispose()

            End Using

        Catch ex As Exception

        End Try

        Return s

    End Function

    Function GetGenres(ByVal url)

        Dim page As String = GetPage(url)

        Return (page)

    End Function

    Public Function getstring(ByVal url As String, ByVal num As Integer) As String

        url = "http://api.shoutcast.com/legacy/genresearch?k=ga1CoUFRd33PBHC0&genre=" + url + "&limit=" + num.ToString

        Dim page As String = GetPage(url)

        Dim extraction As String = ""

        REM Check for stations

        If (page.IndexOf("name=") <> -1) Then

            Dim startIndex As Integer = page.IndexOf("name=") + 6
            Dim endIndex As Integer = page.IndexOf("""", startIndex)
            updatetitle = page.Substring(startIndex, endIndex - startIndex).Trim
            updatetitle = updatetitle.Replace(" - a SHOUTcast.com member station", "")
            page = page.Remove(1, startIndex)
            Dim startTitle As Integer = page.IndexOf("id=") + 4
            Dim endTitle As Integer = page.IndexOf("""", startTitle)
            extraction = page.Substring(startTitle, endTitle - startTitle).Trim
            updateurl = page.Remove(1, endTitle)

            Return (extraction)

        Else

            extraction = "Not Found"
            Return (extraction)

        End If

    End Function

    Public Function gettitle()

        Return (updatetitle)

    End Function

    Public Function getnext()

        Dim Page = updateurl

        Dim extraction As String

        If (Page.IndexOf("name=") <> -1) Then

            Dim startIndex As Integer = Page.IndexOf("name=") + 6
            Dim endIndex As Integer = Page.IndexOf("""", startIndex)
            updatetitle = Page.Substring(startIndex, endIndex - startIndex).Trim
            updatetitle = updatetitle.Replace(" - a SHOUTcast.com member station", "")
            Page = Page.Remove(1, startIndex)
            Dim startTitle As Integer = Page.IndexOf("id=") + 4
            Dim endTitle As Integer = Page.IndexOf("""", startTitle)
            extraction = Page.Substring(startTitle, endTitle - startTitle).Trim
            updateurl = Page.Remove(1, endTitle)

            Return (extraction)

        Else

            extraction = "Not Found"
            Return (extraction)

        End If

    End Function

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim FolderBrowserDialog1 As New FolderBrowserDialog

        With FolderBrowserDialog1

            .RootFolder = Environment.SpecialFolder.Desktop

            .SelectedPath = "C:\"

            .Description = "Select directory to save .pls files"

            If .ShowDialog = DialogResult.OK Then

                TextBox2.Text = (.SelectedPath)

            End If

        End With

    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged

        CheckedListBox1.Items.Clear()

        Dim genre() As String
        genre = GetGenres("http://gazstone.com/radio/shoutcast/genres.txt").Split(Environment.NewLine)

        Dim i As Integer = 0
        For i = 0 To UBound(genre)
            genre(i) = genre(i).Replace(vbCr, "").Replace(vbLf, "")

            CheckedListBox1.Items.Add(genre(i))
        Next

    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged

        CheckedListBox1.Items.Clear()

        Dim genre() As String
        genre = GetGenres("http://gazstone.com/radio/shoutcast/genreslite.txt").Split(Environment.NewLine)

        Dim i As Integer = 0
        For i = 0 To UBound(genre)
            genre(i) = genre(i).Replace(vbCr, "").Replace(vbLf, "")

            CheckedListBox1.Items.Add(genre(i))
        Next

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim i As Integer
        For i = 0 To CheckedListBox1.Items.Count - 1
            CheckedListBox1.SetItemChecked(i, True)
        Next

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        Dim i As Integer
        For i = 0 To CheckedListBox1.Items.Count - 1
            CheckedListBox1.SetItemChecked(i, False)
        Next

    End Sub

End Class